<?php
/**
 * 前台公共文件
 * 
 * @copyright Copyright (c) 2008 sdmblog (http://www.sdmblog.cn)
 * @author sdomain(sdomain@live.cn)
 * @version common.inc.php Sat May 17 17:09:20 CST 2008 php $
 */
// 错误日志类型,可以调定错误提示的级别,发布的时候可以改成error_reporting(0);或是去掉根据PHP自向定义的参数
error_reporting(E_ALL ^ E_NOTICE);

// microtime()的两部分都是以秒为单位返回的, $usec 微秒部分 $sec 秒部分
//list($usec, $sec) = explode(' ', microtime());
// 得到一个微秒级的页面开始执行时间
//$page_time_start = $usec + $sec;
//$page_time_start=bcadd($usec,$sec,4);
//echo $page_time_start;
//数据库查询次数
//$db_query_num=0;

//访调用常量,在防直接调用页面判断此常量是否被定义,如果没被定义,则说明是没有通过此处而直接访问的
define('SDOMAIN', TRUE);
//服务器磁盘根路径
define('ROOT_PATH', substr(dirname(__FILE__), 0, -7));
$virname = substr($_SERVER['SCRIPT_NAME'],0,strrpos($_SERVER['SCRIPT_NAME'],'/')+1);
define('ROOT_WEB',$virname);
header('Content-type: text/html; charset=utf-8');
//尝试获取客户端IP
if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
	$ip = getenv('HTTP_CLIENT_IP');
} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
	$ip = getenv('HTTP_X_FORWARDED_FOR');
} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
	$ip = getenv('REMOTE_ADDR');
} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
	$ip = $_SERVER['REMOTE_ADDR'];
}
// 如果成功匹配IP(7-15个由数字和点组成的字符), 同时过滤一些不明物
preg_match("/[\d\.]{7,15}/", $ip, $ipmatches);
// 上面$ipmatches[0]如果没有取得IP就赋值unknown
$ip = $ipmatches[0] ? $ipmatches[0] : 'unknown';
unset($ipmatches);

//处理不用变量
unset($HTTP_ENV_VARS, $HTTP_POST_VARS, $HTTP_GET_VARS, $HTTP_POST_FILES, $HTTP_COOKIE_VARS, $dbhost, $dbuser, $dbpwd, $dbname, $tablepre);

//加载必要文件
require_once ROOT_PATH .'./include/config.inc.php';
require_once ROOT_PATH .'./include/mysql.cls.php';
require_once ROOT_PATH .'./include/func.lib.php';
require_once ROOT_PATH .'./include/PB_Page.class.php';

// 处理模板
define('SMARTY_DIR', ROOT_PATH. './smarty/');
require SMARTY_DIR.'./Smarty.class.php';
$smarty = new Smarty;
$smarty->template_dir = ROOT_PATH.'./'.$tplpath;
$smarty->compile_dir = ROOT_PATH.'./'.$compilepath;
//$smarty->config_dir = SMARTY_DIR.'./configs/';
$smarty->cache_dir = SMARTY_DIR.'./'.$cachepath;
/* 定义边界符 */
$smarty->left_delimiter = "<{";
$smarty->right_delimiter = "}>";
// 是否开启缓存
//$smarty->caching = false;

//数据库连接
$db = new DB_CLASS;
try {
	$db->connect($dbconfig);
}catch (Exception $e){
	echo '连接数据库出错，错误信息：'.$e->getMessage();
}

//系统配置缓存文件
//此文件是可以删除的,删除后会自动重建
if (!file_exists(ROOT_PATH.'./'.$cachepath.'config/webconfig.php')) {
	if (!loadConfig()) {
		exit('加载配置文件出错,请检查文件夹的读写权限');	
	}
}
require_once ROOT_PATH.'./'.$cachepath.'config/webconfig.php';

//语言文件，日后可能会改，动态的取语言文件的文件名 
require_once ROOT_PATH .'./languages/'.$cfg_bloglanguage.'.php';

// 定义一个密钥--加密用
$sdm_auth_key = md5($secret_id.$_SERVER['HTTP_USER_AGENT']);
list($email, $user_id, $user_money, $user_level) = empty($_COOKIE["{$cookiepre}display"]) ? array('', '', 0, 0) : explode("\t", $_COOKIE["{$cookiepre}display"]);

//显示模式：normal或list cmodel:要创建的MODEL，即通过URL参数传过来的VMODEL从COOKIE里读出来的也就是当前使用的
$vmodel = $_COOKIE[$cookiepre.'vmodel'];
$cmodel = $_GET['vmodel'];
if (isset($cmodel)&& $cmodel!=$vmodel) {
	mkcookie('vmodel',$cmodel,0,false);
	$vmodel = $cmodel;
}
$vmodel=$vmodel?$vmodel:$cfg_viewmodel;

//网站状态
$webstate = GP("webstate");
if($webstate == 0){
	//如果网站关闭,只需要把头尾显示出来即可
	$smarty->assign('webstate',0);
	$smarty->assign('weberror',$lang[WEB_ERROR_0001]);
	//$smarty->display("index.tpl");
	//exit();
}else {
	$smarty->assign('webstate',$webstate);
}
/*
$db_query_num=$db->queryNum;
$smarty->assign('db_query_num',$db_query_num);
*/
$islogin=islogin();
if (!is_array($islogin)) {
	$smarty->assign('islogin',0);
}else {
	$smarty->assign('islogin',1);
	$smarty->assign('logininfo',$islogin);
	include_once(ROOT_PATH.'./model/user.model.php');
	$user = new model_user();
	$usermenu = $user->getUserHTML($islogin);
	$smarty->assign('usermenu',$usermenu);
	unset($usermenu);
	$user = null;
}
unset($islogin);
$smarty->assign('virdir',$virdir);
//URL重写
mod_rewrite();
if ($_GET['act'] =='logout') {
	logout();
	header('Location:/'.$virdir.'index.php');
}
$smarty->assign('blogtitle',$cfg_blogtitle);
$smarty->assign('blogmemo',$cfg_blogmemo);
$smarty->assign('blogurl',$cfg_blogurl);
$smarty->assign('bloglogo',$cfg_bloglogo);
$smarty->assign('showannounce',$cfg_showannounce);

?>